package com.gdth.bank.presenter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import com.gdth.bank.BankApplication;
import com.gdth.bank.bean.DownloadBean;
import com.gdth.bank.bean.SynchronizeBean;
import com.gdth.bank.event.ETCMessageEvent;
import com.gdth.bank.model.DownloadModel;
import com.gdth.bank.model.DzCpKhXxEtcModel;
import com.gdth.bank.model.TXtBmModel;
import com.gdth.bank.view.DzCpKhXxEtcView;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.DzCpkhXxDjEtc;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.database.bean.TXtBm;
import com.gdth.utils.GsonUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

public class DzCpKhXxEtcPresenter {
	private DzCpKhXxEtcView  mDzCpKhXxView;
	private DzCpKhXxEtcModel mDzCpKhXxModel;
	private TXtBmModel mTXtBmModel;
	public int loadDzCpKhXx = 101;
	
	public DzCpKhXxEtcPresenter(DzCpKhXxEtcView mDzCpKhXxView) {
		
		this.mDzCpKhXxView = mDzCpKhXxView;
		mDzCpKhXxModel = new DzCpKhXxEtcModel();
		mTXtBmModel = new TXtBmModel();
	}
	//获取部门下拉数据
	public void getAllBm(String val){
		mTXtBmModel.getAllBm(val)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<List<TXtBm>>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
				}
	
				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					//System.out.println(arg0);
				}
	
				@Override
				public void onNext(List<TXtBm> arg0) {
					// TODO Auto-generated method stub
					mDzCpKhXxView.updateListBm(arg0);
				}
		});
    }
	//登记etc手机本地保存
	public void saveDzcpkhxxetc(DzCpkhXxDjEtc mDzCpkhXxDjEtc){
		mDzCpKhXxModel.saveDzcpkhxxetc(mDzCpkhXxDjEtc)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<List<DzCpkhXxDjEtc>>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					/*mDzCpKhXxView.success();*/
				}

				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mDzCpKhXxView.showMessage("保存本地失败");
				}

				@Override
				public void onNext(List<DzCpkhXxDjEtc> arg0) {
					// TODO Auto-generated method stub
					String str = GsonUtil.getGson().toJson(arg0);
					updateDzcpkhxxetc(str);
					
				}
				
			});
	}
	//etc 数据保存到本地后上传
	public void updateDzcpkhxxetc(String str){
		mDzCpKhXxView.showProgressDialog("上传ETC数据");
		mDzCpKhXxModel.updateDzcpkhxxetc(str)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<String>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mDzCpKhXxView.showMessage("上传成功");
				}

				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mDzCpKhXxView.hideProgressDialog();
					if(arg0.getMessage().equals("车牌号码已存在，请重新填写")){
						mDzCpKhXxView.AlertDialog();
					}else{
					mDzCpKhXxView.showMessage(arg0.getMessage());
					mDzCpKhXxView.showMessage("上传失败");
					mDzCpKhXxView.success();
					}
					
					
				}

				@Override
				public void onNext(String arg0) {
 	       		List<SynchronizeBean> beans = GsonUtil.getSynchronizeBean(arg0);
 	   		    DaoSession session = BankApplication.getDaoSession();
 	   		    SynchronizeBean bean =beans.get(0);
 	      	    String 	appUuid =bean.getWybs();
 	      	    getFjXx(appUuid);
				mDzCpKhXxModel.UpdateZtByDzpEtcBySql(arg0);
				}
				
			});
	}
	public void  getFjXx(String appUuid){
		mDzCpKhXxView.showProgressDialog("上传附件");
		mDzCpKhXxModel.getFjXx(appUuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppFjxxb>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mDzCpKhXxView.hideProgressDialog();
				mDzCpKhXxView.scsuccess();
				mDzCpKhXxView.showMessage("上传成功");
				
				
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.hideProgressDialog();
				mDzCpKhXxView.showMessage("上传失败");
				
			}

			@Override
			public void onNext(List<TAppFjxxb> arg0) {
				// TODO Auto-generated method stub
				for (TAppFjxxb mTAppFjxxb : arg0) {
					DownloadBean bean = new DownloadBean();
					bean.setName(mTAppFjxxb.getWjmc());
					bean.setWjlx(mTAppFjxxb.getWjlx());
					bean.setUuid(mTAppFjxxb.getUuid());
					bean.setWybs(mTAppFjxxb.getWybs());
					bean.setLocalPath(mTAppFjxxb.getAppdz());
					bean.setSqrsfz(mTAppFjxxb.getSfz());
					bean.setScms(0);
					bean.setCjsj(new Date());
					bean.setGnmk(2);
					bean.setZdlx(1);
					DownloadModel.getInstance().upload(bean);
				}
			}
			
		});
	}
	public void getAllDzcpkhxxetc(String val){
		mDzCpKhXxModel.getAllDzcpkhxxetc(val)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<DzCpkhXxDjEtc>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mDzCpKhXxView.hideProgressDialog();
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<DzCpkhXxDjEtc> arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.updateList(arg0);
			   
			}});
	}
   //登记信息未上传查看详情信息
	public void getDzcpkhxxetc(String uuid){
		mDzCpKhXxModel.getDzcpkhxxetc(uuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<DzCpkhXxDjEtc>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mDzCpKhXxView.hideProgressDialog();
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.showMessage("获取失败");
			}

			@Override
			public void onNext(DzCpkhXxDjEtc arg0) {
				// TODO Auto-generated method stub
			  if (arg0 != null){
				  mDzCpKhXxView.updateView(loadDzCpKhXx, arg0);
				}
			}});
	}
	//右键上传
	public void uploadDzcpkhxxetc(String uuid){
		mDzCpKhXxModel.uploadDzcpkhxxetc(uuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<DzCpkhXxDjEtc>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				/*mDzCpKhXxView.hideProgressDialog();*/
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<DzCpkhXxDjEtc> arg0) {
				// TODO Auto-generated method stub
			  if (arg0 != null){
				  String str = GsonUtil.getGson().toJson(arg0);
				 /* updateDzcpkhxxetc(str);*/
				  updateWscDzcpkhxxetc(str);
				}
			}});
	}
	
	//etc 数据保存到本地后上传
		public void updateWscDzcpkhxxetc(String str){
			mDzCpKhXxView.showProgressDialog("正上传数据");
			mDzCpKhXxModel.updateDzcpkhxxetc(str)
				.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Subscriber<String>(){

					@Override
					public void onCompleted() {
						// TODO Auto-generated method stub
						mDzCpKhXxView.showMessage("上传成功");
/*						mDzCpKhXxView.success();
*/						
					}

					@Override
					public void onError(Throwable arg0) {
						// TODO Auto-generated method stub
						mDzCpKhXxView.hideProgressDialog();
						mDzCpKhXxView.showMessage(arg0.getMessage());
						mDzCpKhXxView.showMessage("上传失败");
					}

					@Override
					public void onNext(String arg0) {
						// TODO Auto-generated method stub
						List<SynchronizeBean> beans = GsonUtil.getSynchronizeBean(arg0);
		 	   		    DaoSession session = BankApplication.getDaoSession();
		 	   		    SynchronizeBean bean =beans.get(0);
		 	      	    String 	appUuid =bean.getWybs();
		 	      	    getFjXx(appUuid);
						mDzCpKhXxModel.UpdateZtByDzpEtcBySql(arg0);
						
					}
					
				});
		}
		
	public void deleteDzcpkhxxetc(String appuuid) {
		// TODO Auto-generated method stub
		mDzCpKhXxModel.deleteDzcpkhxxetc(appuuid)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<List<DzCpkhXxDjEtc>>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mDzCpKhXxView.hideProgressDialog();
				}
	
				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mDzCpKhXxView.showMessage("获取失败");
				}
	
				@Override
				public void onNext(List<DzCpkhXxDjEtc> arg0) {
					// TODO Auto-generated method stub
					/*mDzCpKhXxView.updateView(ServerUtil.server_delete, 0);*/
				}
			});
	}
	public void getAllDzcpkhxxetcList(int offset,String time){
		mDzCpKhXxModel.getAllDzcpkhxxetcList(offset,time)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<DzCpkhXxDjEtc>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mDzCpKhXxView.hideProgressDialog();
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<DzCpkhXxDjEtc> arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.updateList(arg0);
			   
			}});
	}
	public void getAllDzcpkhxxetcUpList(int offset , String time){
		mDzCpKhXxModel.getAllDzcpkhxxetcUpList(offset,time)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<DzCpkhXxDjEtc>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mDzCpKhXxView.hideProgressDialog();
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<DzCpkhXxDjEtc> arg0) {
				// TODO Auto-generated method stub
				mDzCpKhXxView.updateListUp(arg0);
			   
			}});
	}
	 public void getDzcpEtcListData(String content){
		 mDzCpKhXxView.showProgressDialog("加载记录中");
		 mDzCpKhXxModel.getDzcpEtcListData(content)
 			.subscribeOn(Schedulers.io())
 			.unsubscribeOn(Schedulers.io())
 			.observeOn(AndroidSchedulers.mainThread())
 			.subscribe(new Subscriber<String>(){

 				@Override
 				public void onCompleted() {
 					// TODO Auto-generated method stub
 					mDzCpKhXxView.hideProgressDialog();
 				}
 	
 				@Override
 				public void onError(Throwable arg0) {
 					// TODO Auto-generated method stub
 					mDzCpKhXxView.hideProgressDialog();
 					mDzCpKhXxView.showMessage(arg0.getMessage());
// 					mDzCpKhXxView.showMessage("加载失败，请检查网络设备");
 					
 				}
 	
 				@Override
 				public void onNext(String arg0) {
 					// TODO Auto-generated method stub
 					JsonArray jsonArray = GsonUtil.getJsonArray(arg0);
 	            	Gson mGson = GsonUtil.getGson();
 	            	List<DzCpkhXxDjEtc> etcList = new ArrayList<DzCpkhXxDjEtc>();
 	       		    for (JsonElement el : jsonArray){
 	       		    DzCpkhXxDjEtc mTDzcpxxEtc = mGson.fromJson(el, DzCpkhXxDjEtc.class);
 	       			etcList.add(mTDzcpxxEtc);
 	       		    }
 	       		mDzCpKhXxView.updateListUp(etcList);
 	            	
 				}
 		});
     }
	//  时间: 2020/4/21 4:48 PM ,作者: xjc, 作用:etc网络删除
	public void getDzcpEtcDelData(String cjrsfz , String uuid){
		mDzCpKhXxView.showProgressDialog("删除中");
		JSONObject val=new JSONObject();
		try {
			val.put("cjrsfz", cjrsfz);
			val.put("uuid", uuid);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mDzCpKhXxModel.getDzcpEtcDelData(val.toString())
				.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Subscriber<String>(){

					@Override
					public void onCompleted() {
						// TODO Auto-generated method stub
						mDzCpKhXxView.hideProgressDialog();
					}

					@Override
					public void onError(Throwable arg0) {
						// TODO Auto-generated method stub
						mDzCpKhXxView.hideProgressDialog();
						mDzCpKhXxView.showMessage(arg0.getMessage());
// 					mDzCpKhXxView.showMessage("加载失败，请检查网络设备");

					}

					@Override
					public void onNext(String arg0) {
						// TODO Auto-generated method stub
						EventBus.getDefault().post(new ETCMessageEvent(ETCMessageEvent.ETCLIST_DEL_SUCCESS,arg0));
					}
				});
	}

	 //未成交下拉数据刷新
	 public void getDzcpEtcUpListData(String content){
		 mDzCpKhXxModel.getDzcpEtcUpListData(content)
 			.subscribeOn(Schedulers.io())
 			.unsubscribeOn(Schedulers.io())
 			.observeOn(AndroidSchedulers.mainThread())
 			.subscribe(new Subscriber<String>(){

 				@Override
 				public void onCompleted() {
 					// TODO Auto-generated method stub
 					
 				}
 	
 				@Override
 				public void onError(Throwable arg0) {
 					// TODO Auto-generated method stub
 					System.out.println("测试");
 					
 				}
 	
 				@Override
 				public void onNext(String arg0) {
 					// TODO Auto-generated method stub
 					JsonArray jsonArray = GsonUtil.getJsonArray(arg0);
 	            	Gson mGson = GsonUtil.getGson();
 	            	List<DzCpkhXxDjEtc> etcList = new ArrayList<DzCpkhXxDjEtc>();
 	       		    for (JsonElement el : jsonArray){
 	       		    DzCpkhXxDjEtc mTDzcpxxEtc = mGson.fromJson(el, DzCpkhXxDjEtc.class);
 	       			etcList.add(mTDzcpxxEtc);
 	       		    }
 	       		mDzCpKhXxView.updateListUp(etcList);
 	            	
 				}
 		});
     }
	 
	 //etc listView已成交上拉列表
	 public void getDzcpEtcYcjListData(String content){
		 mDzCpKhXxView.showProgressDialog("加载记录中");
		 mDzCpKhXxModel.getDzcpEtcYcjListData(content)
 			.subscribeOn(Schedulers.io())
 			.unsubscribeOn(Schedulers.io())
 			.observeOn(AndroidSchedulers.mainThread())
 			.subscribe(new Subscriber<String>(){

 				@Override
 				public void onCompleted() {
 					// TODO Auto-generated method stub
 					mDzCpKhXxView.hideProgressDialog();
 				}
 	
 				@Override
 				public void onError(Throwable arg0) {
 					// TODO Auto-generated method stub
 					mDzCpKhXxView.showMessage("加载失败，请检查网络设备");
 					
 				}
 	
 				@Override
 				public void onNext(String arg0) {
 					// TODO Auto-generated method stub
 					JsonArray jsonArray = GsonUtil.getJsonArray(arg0);
 	            	Gson mGson = GsonUtil.getGson();
 	            	List<DzCpkhXxDjEtc> etcList = new ArrayList<DzCpkhXxDjEtc>();
 	       		    for (JsonElement el : jsonArray){
 	       		    DzCpkhXxDjEtc mTDzcpxxEtc = mGson.fromJson(el, DzCpkhXxDjEtc.class);
 	       			etcList.add(mTDzcpxxEtc);
 	       		    }
 	       		mDzCpKhXxView.updateListUp(etcList);
 	            	
 				}
 		});
     }
	 //etc listView已成交下拉列表
	 public void getDzcpEtcYcjUpListData(String content){
		 mDzCpKhXxModel.getDzcpEtcYcjUpListData(content)
 			.subscribeOn(Schedulers.io())
 			.unsubscribeOn(Schedulers.io())
 			.observeOn(AndroidSchedulers.mainThread())
 			.subscribe(new Subscriber<String>(){

 				@Override
 				public void onCompleted() {
 					// TODO Auto-generated method stub
 					
 				}
 	
 				@Override
 				public void onError(Throwable arg0) {
 					// TODO Auto-generated method stub
 					System.out.println("测试");
 					
 				}
 	
 				@Override
 				public void onNext(String arg0) {
 					// TODO Auto-generated method stub
 					JsonArray jsonArray = GsonUtil.getJsonArray(arg0);
 	            	Gson mGson = GsonUtil.getGson();
 	            	List<DzCpkhXxDjEtc> etcList = new ArrayList<DzCpkhXxDjEtc>();
 	       		    for (JsonElement el : jsonArray){
 	       		    DzCpkhXxDjEtc mTDzcpxxEtc = mGson.fromJson(el, DzCpkhXxDjEtc.class);
 	       			etcList.add(mTDzcpxxEtc);
 	       		    }
 	       		mDzCpKhXxView.updateListUp(etcList);
 	            	
 				}
 		});
     }
	 public void serachXx(String content){
		 mDzCpKhXxModel.serachXx(content)
 			.subscribeOn(Schedulers.io())
 			.unsubscribeOn(Schedulers.io())
 			.observeOn(AndroidSchedulers.mainThread())
 			.subscribe(new Subscriber<String>(){

 				@Override
 				public void onCompleted() {
 					// TODO Auto-generated method stub
 					
 				}
 	
 				@Override
 				public void onError(Throwable arg0) {
 					// TODO Auto-generated method stub
 					System.out.println("测试");
 					
 				}
 	
 				@Override
 				public void onNext(String arg0) {
 					// TODO Auto-generated method stub
 				//System.out.println(arg0);
 				JsonArray jsonArray = GsonUtil.getJsonArray(arg0);
 				Gson gson =GsonUtil.getGson();
 				DzCpkhXxDjEtc  mDzCpkhXxDjEtc= new DzCpkhXxDjEtc();
	            JsonElement el = jsonArray.get(0);
	       		DzCpkhXxDjEtc mTDzcpxxEtc = gson.fromJson(el, DzCpkhXxDjEtc.class);
	       		 mDzCpKhXxView.updateView(loadDzCpKhXx, mTDzcpxxEtc);
 				}
 		});
     }
	 public void getBdserach(String content){
		 mDzCpKhXxView.showProgressDialog("加载记录中");
		 mDzCpKhXxModel.getBdserach(content)
 			.subscribeOn(Schedulers.io())
 			.unsubscribeOn(Schedulers.io())
 			.observeOn(AndroidSchedulers.mainThread())
 			.subscribe(new Subscriber<String>(){

 				@Override
 				public void onCompleted() {
 					// TODO Auto-generated method stub
 					mDzCpKhXxView.hideProgressDialog();
 				}
 	
 				@Override
 				public void onError(Throwable arg0) {
 					// TODO Auto-generated method stub
 					mDzCpKhXxView.hideProgressDialog();
 					mDzCpKhXxView.showMessage(arg0.getMessage());
// 					mDzCpKhXxView.showMessage("加载失败");
 					
 				}
 	
 				@Override
 				public void onNext(String arg0) {
 					// TODO Auto-generated method stub
 				//System.out.println(arg0);
 				JsonArray jsonArray = GsonUtil.getJsonArray(arg0);
 				Gson gson =GsonUtil.getGson();
 				List<DzCpkhXxDjEtc> mDzCpkhXxDjEtcList = new ArrayList<DzCpkhXxDjEtc>();
 				for(JsonElement el : jsonArray){
 					DzCpkhXxDjEtc  mDzCpkhXxDjEtc= gson.fromJson(el, DzCpkhXxDjEtc.class);
 					mDzCpkhXxDjEtcList.add(mDzCpkhXxDjEtc);
 				}
 				
	       		 mDzCpKhXxView.updateListUp(mDzCpkhXxDjEtcList);
 				}
 		});
     }
     public void getBdYcjserach(String content){
    	 mDzCpKhXxView.showProgressDialog("加载记录中");
		 mDzCpKhXxModel.getBdYcjserach(content)
 			.subscribeOn(Schedulers.io())
 			.unsubscribeOn(Schedulers.io())
 			.observeOn(AndroidSchedulers.mainThread())
 			.subscribe(new Subscriber<String>(){

 				@Override
 				public void onCompleted() {
 					// TODO Auto-generated method stub
 					mDzCpKhXxView.hideProgressDialog();
 				}
 	
 				@Override
 				public void onError(Throwable arg0) {
 					// TODO Auto-generated method stub
 					mDzCpKhXxView.showMessage("加载失败");
 					
 					
 				}
 	
 				@Override
 				public void onNext(String arg0) {
 					// TODO Auto-generated method stub
 				//System.out.println(arg0);
 				JsonArray jsonArray = GsonUtil.getJsonArray(arg0);
 				Gson gson =GsonUtil.getGson();
 				List<DzCpkhXxDjEtc> mDzCpkhXxDjEtcList = new ArrayList<DzCpkhXxDjEtc>();
 				for(JsonElement el : jsonArray){
 					DzCpkhXxDjEtc  mDzCpkhXxDjEtc= gson.fromJson(el, DzCpkhXxDjEtc.class);
 					mDzCpkhXxDjEtcList.add(mDzCpkhXxDjEtc);
 				}
 				
	       		 mDzCpKhXxView.updateListUp(mDzCpkhXxDjEtcList);
 				}
 		});
     }
}   

